package com.dataimport.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.StudentClassInfo;

import com.dataimport.entity.targetentity.Classes;
import com.dataimport.mapper.StudentClassInfoMapper;
import com.dataimport.service.ClassesService;
import com.dataimport.service.StudentClassInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xin
 * @date 2022/9/27 22:32
 */
@Service
public class StudentClassInfoServiceImpl extends ServiceImpl<StudentClassInfoMapper, StudentClassInfo> implements StudentClassInfoService {

    @Autowired
    private StudentClassInfoMapper studentClassInfoMapper;

    @Autowired
    private ClassesService classesService;

    public List<Classes> selectData(Long currentPage){
        //开启数据库2
        DynamicDataSourceContextHolder.push("mysql2");
        //查询全部班级
        Page<StudentClassInfo> page = new Page<>(currentPage,1000);
        Page<StudentClassInfo> studentClassInfoPage = studentClassInfoMapper.selectPage(page, null);
        List<StudentClassInfo> studentClassInfos = studentClassInfoPage.getRecords();
        //过滤数据
        List<Classes> classesList = studentClassInfos.stream().parallel()
                .filter(studentClassInfo -> StringUtils.hasText(studentClassInfo.getBjbm()) && StringUtils.hasText(studentClassInfo.getYxsh()) && StringUtils.hasText(studentClassInfo.getZybm()))
                .map(studentClassInfo -> {
                    Classes classes = new Classes();
                    classes.setClassesId(studentClassInfo.getBjbm());
                    classes.setClassesName(studentClassInfo.getBjmc());
                    classes.setDepartmentCode(studentClassInfo.getYxsh());
                    classes.setMajorCode(studentClassInfo.getZybm());
                    classes.setClassesStatus(Integer.valueOf(studentClassInfo.getSfky()));
                    return classes;
                }).collect(Collectors.toList());

//        classesList.forEach(System.out::println);
        DynamicDataSourceContextHolder.poll();
        return classesList;
    }

    @Override
    public void dealWithStudentClassInfo() {
        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<Classes> classes = selectData(currentPage);
            if (ObjectUtils.isEmpty(classes)) {
                break;
            }
            insert(classes);
            System.out.println("插入成功");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(List<Classes> classesList){
        //开启数据库1
        DynamicDataSourceContextHolder.push("mysql1");

        classesService.saveBatch(classesList);
        DynamicDataSourceContextHolder.poll();
    }
}
